/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package facades;

import entities.Ticket;
import entities.Usuario;
import java.util.List;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author damfercer
 */
@Stateless
public class TicketFacade extends AbstractFacade<Ticket> {
    @Inject
    LineaTicketFacade lineaTicketFacade;
            
    @PersistenceContext(unitName = "CorporatePU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public TicketFacade() {
        super(Ticket.class);
    }
    
    public List<Ticket> findByTiendasDeUsuario(Usuario u){
        StringBuilder stringBuilder=new StringBuilder();
        stringBuilder.append("SELECT DISTINCT ticket.* FROM ticket,usuario_tienda,caja WHERE ticket.CAJA=caja.ID_CAJA AND caja.TIENDA=usuario_tienda.TIENDA AND usuario_tienda.USUARIO=");
        stringBuilder.append(u.getIdUsuario().toString());
        List<Ticket> tickets=em.createNativeQuery(stringBuilder.toString(), Ticket.class).getResultList();
        return tickets;
    }
    
}
